package com.pug.web.user;

import com.pug.bo.AvatarBo;
import com.pug.bo.BgImgBo;
import com.pug.bo.KssUserBo;
import com.pug.config.minio.MinIOConfig;
import com.pug.config.minio.MinIoUploadService;
import com.pug.pojo.KssUser;
import com.pug.service.user.IUserService;
import com.pug.utils.fn.asserts.Vsserts;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.UUID;

/**
 * @author 飞哥
 * @Title: 学相伴出品
 * @Description: 飞哥B站地址：https://space.bilibili.com/490711252
 * 记得关注和三连哦！
 * @Description: 我们有一个学习网站：https://www.kuangstudy.com
 * @date 2022/3/16$ 23:56$
 */
@RestController
@RequiredArgsConstructor
public class UserController {

    private final IUserService userService;
    @Resource
    MinIoUploadService minIoUploadService;
    @Resource
    MinIOConfig minIOConfig;
    @GetMapping("/getuser/{id}")
    public KssUser getUser(@PathVariable("id")Long id){
        return userService.getById(id);
    }
    //更改头像
    @PostMapping("/api/user/modifty/avatar/{userId}")
    @Transactional(propagation = Propagation.REQUIRED)
    public AvatarBo changeUserAvatar(@PathVariable("userId") Long userId, @RequestParam("filename") String fileName, MultipartFile file) throws Exception {
        UUID uuid = UUID.randomUUID();
        String s = uuid.toString(); //随机名称
        String oldName=file.getOriginalFilename();
        String exit=oldName.substring(oldName.lastIndexOf('.'));
        minIoUploadService.uploadFile("lqx",s+exit,file.getInputStream());
        String imgUrl = minIOConfig.getFileHost()
                + "/"
                + minIOConfig.getBucketName()
                + "/"
                + s;

        //根据id修改用户的头像
        userService.updateBgImgById(userId,imgUrl+exit);
        return new AvatarBo(imgUrl+exit);
    }
    //更改背景
    @PostMapping("/api/user/modifty/bgimg/{userId}")
    @Transactional(propagation = Propagation.REQUIRED)
    public BgImgBo changeUserBgImg(@PathVariable("userId")Long userId,String filename,String bucketname,MultipartFile file) throws Exception {
        String s = UUID.randomUUID().toString();//随机背景图片名字
        String exit=file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf('.'));
        s=s+exit;
        minIoUploadService.uploadFile("lqx",s,file.getInputStream());
        String imgUrl = minIOConfig.getFileHost()
                + "/"
                + minIOConfig.getBucketName()
                + "/"
                + s;
        userService.updateBgImgById(userId,imgUrl);
        return new BgImgBo(imgUrl);
    }

//    @ApiOperation("修改用户其他的信息")
//    @PostMapping("/user/modifty/info")
//    public KssUserBo updateUserInfo(@RequestBody KssUserSettingVo userSettingVo) throws Exception {
//        Vsserts.isNullEx(userSettingVo.getUserId(), "没有找到修改用户");
//        KssUser kssUser = userService.getById(userSettingVo.getUserId());
//        BeanUtils.copyProperties(userSettingVo, kssUser);
//        kssUser.setId(userSettingVo.getUserId());
//        userService.updateById(kssUser);
//        // 返回
//        KssUserBo kssUserBo = new KssUserBo();
//        BeanUtils.copyProperties(kssUser, kssUserBo);
//        return kssUserBo;
//    }


//    @ApiOperation("同步微信授权的用户信息")
//    @PostMapping("/user/update/weixin")
//    public KssUserBo updateweixinuserinfo(@RequestBody WeixinData weixinData) throws Exception {
//        KssUser kssUser = UserThrealLocal.get();//这里确实完整的信息 20 update
//        KssUser modifyUser = new KssUser();
//        modifyUser.setId(kssUser.getId());
//        modifyUser.setNickname(weixinData.getNickName());
//        modifyUser.setAvatar(weixinData.getAvatarUrl());
//        modifyUser.setCountry(weixinData.getCountry());
//        modifyUser.setProvince(weixinData.getProvince());
//        modifyUser.setCity(weixinData.getCity());
//        modifyUser.setSex(Integer.parseInt(weixinData.getGender()));
//        boolean b = userService.updateById(modifyUser);
//        if (b) {
//            // 返回
//            KssUserBo kssUserBo = new KssUserBo();
//            BeanUtils.copyProperties(modifyUser, kssUserBo);
//            return kssUserBo;
//        }
//        return null;
//    }
}
